package cn.item.buckle;

import cn.item.buckle.confg.ConfigEnum;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class GenDataUtil {

	public static void main(String[] args) {
		List<String[]> dataList = createFlowActionData();
		System.out.println(dataList.size());
		if (dataList != null) {
			createCameraInfoData(dataList);
		}
	}

	public static List<String[]> createFlowActionData() {
		PrintWriter writer;
		try {
			writer = new PrintWriter(new FileOutputStream(new File(ConfigEnum.LOCAL_DATA_DIR.getValue() + ConfigEnum.MONITOR_FLOW_ACTION.getValue() + ".txt")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}

		String[] locations = new String[] { "鲁", "京", "京", "京", "沪", "京", "京", "深", "京", "京" };

		String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

		List<String[]> dataList = new ArrayList<>();
		Random random = new Random();
		//模拟30000个车辆
		for (int i = 0; i < 300000; i++) {
			String car = locations[random.nextInt(10)] + (char) (65 + random.nextInt(26)) + fulfuill(random.nextInt(100000) + "", 5);
			//baseActionTime 模拟24小时
			String baseActionTime = date + " " + fulfuill(random.nextInt(23) + "", 2);

			//这里的for循环模拟每辆车经过不同的卡扣不同的摄像头的数据。
			for (int j = 0; j < random.nextInt(300) + 1; j++) {
				//模拟每个车辆每被30个摄像头拍摄后 时间上累计加1小时。这样做使数据更加真实。
				if (j % 30 == 0 && j != 0) {
					baseActionTime = date + " " + fulfuill((Integer.parseInt(baseActionTime.split(" ")[1]) + 1) + "", 2);
					//模拟经过此卡扣开始时间
					String actionTime = baseActionTime + ":"
							+ fulfuill(random.nextInt(60) + "", 2) + ":"
							+ fulfuill(random.nextInt(60) + "", 2);

					//模拟9个卡扣monitorId
					String monitorId = fulfuill(random.nextInt(9) + "", 4);
					//模拟速度
					String speed = random.nextInt(260) + 1 + "";
					//模拟道路id 【1~50 个道路】
					String roadId = random.nextInt(50) + 1 + "";
					//模拟摄像头id cameraId
					String cameraId = fulfuill(random.nextInt(100000) + "", 5);
					//模拟areaId 【一共8个区域】
					String areaId = fulfuill(random.nextInt(8) + 1 + "", 2);

					String content = date + "\t" + monitorId + "\t" + cameraId + "\t" + car + "\t" + actionTime + "\t" + speed + "\t" + roadId + "\t" + areaId;
					writer.write(content);
					writer.write("\n");
					dataList.add(new String[] { monitorId, cameraId });
				}
			}
		}
		writer.flush();
		writer.close();
		return dataList;
	}

	/**
	 * 创建卡扣对应摄像头的基本关系表数据
	 */
	public static Map<String, Set<String>> createCameraInfoData() {
		Map<String, Set<String>> monitorAndCameras = new HashMap<>();
		Random random = new Random();
		String monitorId = fulfuill(random.nextInt(9) + "", 4);
		return monitorAndCameras;
	}

	/**
	 * 创建卡扣对应摄像头的基本关系表数据
	 */
	public static void createCameraInfoData(List<String[]> dataList) {
		//生成 monitor_id 对应camera_id表
		Map<String, Set<String>> monitorAndCameras = new HashMap<>();
		Random random = new Random();
		int index = 0;
		for (String[] arr : dataList) {
			Set<String> sets = monitorAndCameras.computeIfAbsent(arr[0], k -> new HashSet<>());
			if (++index % 1000 == 0) {
				//这里每隔1000条数据随机插入一条数据，模拟出来标准表中卡扣对应摄像头的数据。这个摄像头的数据不一定会在车辆数据中有。
				sets.add(fulfuill(random.nextInt(100000) + "", 5));
			}
			sets.add(arr[1]);
		}
		PrintWriter writer;
		try {
			writer = new PrintWriter(new FileOutputStream(new File(ConfigEnum.LOCAL_DATA_DIR.getValue() + ConfigEnum.MONITOR_CAMERA_INFO.getValue() + ".txt")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		monitorAndCameras.forEach((monitorId, v) -> {
			v.forEach(cameraId -> {
				String content = monitorId + "\t" + cameraId;
				writer.write(content);
				writer.write("\n");
			});
		});
		writer.flush();
		writer.close();
	}

	public static String fulfuill(String s, int num) {
		if (s.length() < num) {
			for (int i = s.length(); i < num; i++) {
				s = "0".concat(s);
			}
		}
		return s;
	}

}
